package com.barneyx.mshinfoplatform.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.barneyx.mshinfoplatform.common.ExResult;
import com.barneyx.mshinfoplatform.entity.area.AreaEntity;
import com.barneyx.mshinfoplatform.service.area.IAreaEntityService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/aera/")
public class AreaController {
    @Resource
    private IAreaEntityService areaService;

    @GetMapping("list")
    public ExResult<?> getList() {
        return ExResult.ofAuto(areaService.list());
    }

    @GetMapping("detail")
    public ExResult getDetail(@RequestParam("areaid") String areaId) {
        return ExResult.ofAuto(areaService.getById(areaId));
    }


    @GetMapping("default-area")
    public ExResult<?> getDefaultArea() {
        return ExResult.ofAuto(areaService.getOne(new LambdaQueryWrapper<AreaEntity>()
                .eq(AreaEntity::getAreaDefault, true)));
    }

    @PostMapping("add")
    public ExResult createArea(@RequestBody AreaEntity area) {
        return ExResult.ofAuto(areaService.save(area), area);
    }

    @PutMapping("update")
    public ExResult updateArea(@RequestBody AreaEntity area) {
        return ExResult.ofAuto(areaService.updateById(area), area);
    }

    @Delete("delete")
    public ExResult deleteById(@RequestParam("areaid") String areaId) {
        return ExResult.ofAuto(areaService.removeById(areaId));
    }
}
